
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System.Collections.Generic;
using System.Windows.Forms;
using FlowLib.Containers;
using ClientExample.Interfaces;

namespace ClientExample.Controls
{
    public class ListViewExtended<T> : System.Windows.Forms.ListView
    {
        protected delegate void UpdateCountDelegate();

        protected FlowSortedList<T> filterItems = new FlowSortedList<T>();
        protected IListViewExtendedFilter<T> filter = null;
        protected int visibleStart = 0;
        protected int visibleLength = 0;

        public IListViewExtendedFilter<T> Filter
        {
            get { return filter; }
            set
            {
                filter = value;
                if (filter != null)
                    filterItems = filter.FilterAll(new FlowSortedList<T>(AllItems, filterItems.Comparer));
                else
                    filterItems = new FlowSortedList<T>(AllItems);
            }
        }

        public IComparer<T> Comparer
        {
            get;
            set;
        }

        public IConvertToListViewItem<T> Converter
        {
            get;
            set;
        }

        public FlowSortedList<T> AllItems
        {
            get;
            protected set;
        }

        public new FlowSortedList<T> Items
        {
            get
            {
                return filterItems;
            }
            protected set
            {
                filterItems = value;
            }
        }

        public FlowSortedList<T> VisibleItems
        {
            get
            {
                FlowSortedList<T> list = new FlowSortedList<T>(Items.Comparer);
                try
                {
                    int start = visibleStart;
                    int length = visibleLength;
                    int pos = visibleStart;
                    while (pos < start + length)
                    {
                        list.Add(Items[pos++]);
                    }
                }
                catch { }
                return list;
            }
        }

        public ListViewExtended()
        {
            AllItems = new FlowSortedList<T>();
            AllItems.TriggerEvents = true;
            AllItems.ItemAdded += new FlowSortedList<T>.SingleChangedDelegate(AllItems_ItemAdded);
            AllItems.ItemRemoved += new FlowSortedList<T>.SingleChangedDelegate(AllItems_ItemRemoved);
            AllItems.ItemsRemoved += new FlowSortedList<T>.ClearedDelegate(AllItems_ItemsRemoved);
            this.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(ListViewExtended_RetrieveVirtualItem);
            this.CacheVirtualItems += new CacheVirtualItemsEventHandler(ListViewExtended_CacheVirtualItems);
        }

        void ListViewExtended_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
        {
            visibleStart = e.StartIndex;
            visibleLength = e.EndIndex - e.StartIndex;
        }

        void AllItems_ItemsRemoved(int prevCount)
        {
            Items.Clear();
            UpdateCount();
        }

        void AllItems_ItemRemoved(int pos, T item)
        {
            if (Filter != null && Filter.Filter(item))
            {
                Items.Remove(item);
                UpdateCount();
            }
        }

        void AllItems_ItemAdded(int pos, T item)
        {
            if (Filter != null && Filter.Filter(item))
            {
                Items.Add(item);
                UpdateCount();
            }
        }

        protected void UpdateCount()
        {
            if (this.InvokeRequired)
                this.Invoke(new UpdateCountDelegate(OnUpdateCount));
            else
                OnUpdateCount();
        }

        protected void OnUpdateCount()
        {
            this.VirtualListSize = Items.Count;
        }

        void ListViewExtended_RetrieveVirtualItem(object sender, System.Windows.Forms.RetrieveVirtualItemEventArgs e)
        {
            lock (filterItems)
            {
                if (filterItems.Count > e.ItemIndex)
                {
                    e.Item = Converter.Convert(filterItems[e.ItemIndex]);
                }
                else
                {
                    e.Item = new System.Windows.Forms.ListViewItem("Error");
                }
            }
        }
    }
}
